package com.iwop.moto.controller;

import com.iwop.moto.auth.LoginAuth;
import com.iwop.moto.comm.ErrCode;
import com.iwop.moto.model.ResultModel;
import com.iwop.moto.model.SuperRecordModel;
import com.iwop.moto.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * liuwb 2016-07-20 08:57
 */
@Controller
public class CollectController extends BaseController {

    @Autowired
    private CollectService collectService;

    /**
     * liuwb 2016-07-20 09:45
     * 功能:添加收藏夹
     * 参数:userId  用户ID
     * 参数:goodsId 收藏的产品ID
     * 参数:coltype 收藏的产品类型
     */
    @RequestMapping(value = "/api/collect/add", method = {RequestMethod.POST})
    @ResponseBody
    //@LoginAuth(isval = true)
    public ResultModel addCollect() {
        SuperRecordModel SRModel = this.getSRModel();
        try {
            SRModel.put("sid", UUID.randomUUID().toString());
            SRModel.put("addtime", new Date());
            SRModel.put("status", "1");
            collectService.insertCollect(SRModel);
            return getSucceed(ErrCode.SUCCEED);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * liuwb 2016-07-20 09:50
     * 功能:移除收藏夹(逻辑删除)
     * 参数:sid 主键
     */
    @RequestMapping(value = "/api/collect/update/status", method = {RequestMethod.POST})
    @ResponseBody
    //@LoginAuth(isval = true)
    public ResultModel updateSatausBySid() {
        SuperRecordModel SRModel = this.getSRModel();
        try {
            String sids = SRModel.getString("sids");
            String[] sidArr = sids.split(",");
            for (String sid : sidArr) {
                SuperRecordModel srm = new SuperRecordModel();
                srm.put("sid", sid);
                collectService.updateSatausBySid(srm);
            }
            return getSucceed(ErrCode.SUCCEED);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * 功能:我的收藏夹列表
     * 参数:userId 用户id
     * 参数:coltype 0 货品 1 供应商
     */
    @RequestMapping(value = "/api/collect/list", method = {RequestMethod.POST})
    @ResponseBody
    //@LoginAuth(isval = true)
    public ResultModel findCollectByUserId() {
        SuperRecordModel SRModel = this.getSRModel();
        try {
            List<SuperRecordModel> lists = new ArrayList<>();
            if (SRModel.getString("coltype").equals("0")) {
                lists = collectService.findCollectGoods(SRModel);
            } else if (SRModel.getString("coltype").equals("1")) {
                lists = collectService.findCollectSupplier(SRModel);
            }
            if (lists.size() == 0) {
                return getErr(ErrCode.ERROR_NOCOUNT);
            }
            return getSucceed(lists);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }


}
